/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mb.monitor;

import java.net.ServerSocket;
import mb.config.MonitorConfig;
import mb.mbmiddle.MbPartMiddle;
import mb.utils.LogText;

/**
 *
 * @author thanh.nguyenchi
 */
public class MonitorListener extends Thread {
    MbPartMiddle objMain;
    ServerSocket listener;
    int clientNumber;
    LogText objLog;
    MonitorConfig objConfig;
    
    public MonitorListener(int port,MbPartMiddle objMain, LogText objLog,MonitorConfig objConfig) {
        this.objMain = objMain;
        this.objLog = objLog;
        this.objConfig = objConfig;
        try{
            listener = new ServerSocket(port); 
            log("Start monitor handler on port " + port);
        }
        catch(Exception ex){
            log("Can't create monitor socket on port " + port + " [Err]:" + ex.toString());
        }
    }
    
    public void run() {
        try{
            while (true) {
                new MonitorHandler(listener.accept(),clientNumber++,objMain,objLog,this.objConfig).start();
            }
        }
        catch(Exception ex){
            log("Monitor listen [Err]:" + ex.toString());
            try{
                listener.close();
            }
            catch(Exception exo){
                
            }
        }
        
    }
        
    private void log(String message) {
        objLog.log(message);
    }
    
}
